package net.aaa.hotel.controller;

import net.aaa.hotel.dao.GustRoomDao;
import net.aaa.hotel.dao.HTypeDao;
import net.aaa.hotel.dao.ITorderDao;
import net.aaa.hotel.dao.TRuzhudetailDao;
import net.aaa.hotel.entity.PageInfo;
import net.aaa.hotel.entity.TRuzhudetail;
import net.aaa.hotel.util.LayuiTable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/order")
public class OrderController {
    @Autowired
    HTypeDao hTypeDao;
    @Autowired
    ITorderDao torderDao;
    @Autowired
    GustRoomDao gustRoomDao;
    @Autowired
    TRuzhudetailDao ruzhudetailDao;
    /**
     * 先获得到对应的房间类型信息
     * 再获得到对应id的房间的所有可用的房间号
     */
    @RequestMapping("roomtype")
    @ResponseBody
    public List<Map> getRoomType(){
        List<Map> mapList = hTypeDao.queryAllInfo();
        return mapList;
    }
    /**
     * 获取所有的订单信息
     */
    @RequestMapping("orderInfo")
    @ResponseBody
    public LayuiTable getAll(PageInfo info){
        List<Map> pageInfo = torderDao.getPageInfo(info);
        int count = torderDao.getCount(info.getCondition());
//        List<Map> allInfo = torderDao.getAllInfo();
        LayuiTable table = new LayuiTable(count,pageInfo);
        return table;
    }

    @RequestMapping("getRoomByTid")
    @ResponseBody
    public List<?> getRoom(Integer id,String status){
        List rooms = gustRoomDao.getRoomById(id,status);
        return rooms;
    }


    @RequestMapping("rzdetail")
    @ResponseBody
    /**
     * 入住详情,以及标记房间的状态,订单的状态
     */
    public String getRzDetail(TRuzhudetail ruzhudetail){
        // System.out.println(ruzhudetail);
        int detail = ruzhudetailDao.addRuzhuDetail(ruzhudetail);
        int room = gustRoomDao.changeRoomStatusById(2, ruzhudetail.getRoomid());
        // 当数量为count时改变订单的状态
        Map info = torderDao.getInfoByOrderid(ruzhudetail.getOrderid());
        // 入住详情中的对应订单的数量
        int anInt = Integer.parseInt(info.get("a") + "");
        // 订单中预定的房间数量
        int countInt = Integer.parseInt(info.get("count") + "");
        // 当anInt>=countInt时说明该订单的房间已经分配完成了
        int order = 0;
        if(anInt>=countInt){
            order = torderDao.changeOrderStatus(1, ruzhudetail.getOrderid());
        }
        return detail+room+order+"";
    }
}
